﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Model;

namespace BLL.backStage
{
   public class PostManager
    {
        private static int i = 1;

        public static Boolean addPost(Dictionary<int, string> postdic)
        {
            //i = i + 1;
            Boolean flag = true;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/xml/Post.xml"));
            XmlNode root = xmlDoc.SelectSingleNode("Movies");
            i = Int32.Parse(((XmlElement)root).GetAttribute("lastId"));
            i += 1;
            XmlElement xe1 = xmlDoc.CreateElement("Movie"); 
            xe1.SetAttribute("id", i.ToString());
            xe1.SetAttribute("title", postdic[0]);
            xe1.SetAttribute("date", postdic[1]);
            xe1.SetAttribute("content", postdic[2]);

//             XmlElement xesub1 = xmlDoc.CreateElement("title");
//             xesub1.InnerText = postdic[0];//设置文本节点 
//             xe1.AppendChild(xesub1);
//             XmlElement xesub2 = xmlDoc.CreateElement("date");
//             xesub2.InnerText = postdic[1];
//             xe1.AppendChild(xesub2);
//             XmlElement xesub3 = xmlDoc.CreateElement("content");
//             xesub3.InnerText = postdic[2];
//             xe1.AppendChild(xesub3);

            root.AppendChild(xe1);
            ((XmlElement)root).SetAttribute("lastId", i.ToString());
            xmlDoc.Save(HttpContext.Current.Server.MapPath("/xml/Post.xml"));
            return flag;
        }

        public static Boolean delNews(string id)
        {
            Boolean flag = true;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/xml/Post.xml"));
            XmlNode root = xmlDoc.SelectSingleNode("Movies");
            XmlNodeList xnl = xmlDoc.SelectSingleNode("Movies").ChildNodes;
            for (int i = 0; i < xnl.Count; i++)
            {
                XmlElement xe = (XmlElement)xnl.Item(i);
                if (xe.GetAttribute("id") == id)
                {
                    root.RemoveChild(xe);
                    if (i < xnl.Count)
                        i = i - 1;
                }
            }
            xmlDoc.Save(HttpContext.Current.Server.MapPath("/xml/Post.xml"));
            return flag;

        }


        public static Boolean updPost(Dictionary<int, string> postdic)
        {
            Boolean flag = true;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/xml/Post.xml"));
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("Movies").ChildNodes;
            foreach (XmlNode xn in nodeList)//遍历所有子节点 
            {
                XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 
                if (xe.GetAttribute("id") == postdic[0])
                {

                    xe.SetAttribute("title", postdic[1]);
                    xe.SetAttribute("date", postdic[2]);
                    xe.SetAttribute("content", postdic[3]);

//                     XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 
//                     foreach (XmlNode xn1 in nls)//遍历 
//                     {
//                         XmlElement xe2 = (XmlElement)xn1;//转换类型 
//                         if (xe2.Name == "title")//如果找到 
//                         {
//                             xe2.InnerText = postdic[1];//则修改
//                         }
// 
//                         if (xe2.Name == "date")//如果找到 
//                         {
//                             xe2.InnerText = postdic[2];//则修改
//                         }
// 
//                         if (xe2.Name == "content")//如果找到 
//                         {
//                             xe2.InnerText = postdic[3];//则修改
//                         }
//                     }
                }
                xmlDoc.Save(HttpContext.Current.Server.MapPath("/xml/Post.xml"));//保存。
            }
            return flag;
        }

        public static List<Post> searchPost(string title)
        {
            List<Post> postlist = new List<Post>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/xml/Post.xml"));
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("Movies").ChildNodes;
            foreach (XmlNode xn in nodeList)//遍历所有子节点 
            {
                XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 
                Post post = new Post();
                Boolean flag = false;
                flag = isFetch(xe, title);
                if (flag == true)
                {
                    post.Id = xe.GetAttribute("id");
                    post.Date = xe.GetAttribute("date");
                    post.Title = xe.GetAttribute("title");
                    post.Content = xe.GetAttribute("content");
//                     XmlNodeList nls = xe.ChildNodes;
//                     foreach (XmlNode xn1 in nls)//遍历 
//                     {
//                         XmlElement xe2 = (XmlElement)xn1;//转换类型 
//                         if (xe2.Name == "title")
//                         {
//                             post.Title = xe2.InnerText;
//                         }
// 
//                         if (xe2.Name == "date")
//                         {
//                             post.Date = xe2.InnerText;
//                         }
// 
//                         if (xe2.Name == "content")
//                         {
//                             post.Content = xe2.InnerText;
//                         }
//                     }
                    postlist.Add(post);
                }

            }
            //xmlDoc.Save(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml"));//保存。
            return postlist;
        }


        public static Boolean isFetch(XmlElement xe, string title)
        {
            Boolean flag = false;
            XmlNodeList nls = xe.ChildNodes;
            if (title != null && title.Length > 0)
            {
                    foreach (XmlNode xn1 in nls)
                    {
                        XmlElement xe2 = (XmlElement)xn1;
                        if (xe2.Name == "title" && xe2.InnerText == title)
                        {
                           flag = true;
                        }
                       
                    }
                    
                }
                else
                {
                   
                            flag = true;
                     
                 }
              
          
            return flag;
        }

        public static int FillSearchTable(Table table, string title, int pageNo)
        {
            table.BorderWidth = 1;
            List<Post> postlist = searchPost(title);
            int beginItemNo = (pageNo - 1) * 5;
            int endItemNo = ((pageNo - 1) * 5 + 5) < (postlist.Count) ? ((pageNo - 1) * 5 + 5) : (postlist.Count);
            TableRow tr0 = new TableRow();
            for (int i = beginItemNo; i < endItemNo; ++i)
            {
                Post post = postlist[i];

                TableRow tr = new TableRow();
                TableCell[] tc = new TableCell[6];

                for (int j = 0; j < 6; ++j)
                {
                    tc[j] = new TableCell();
                }

                tc[0].Text = post.Id;
                tc[0].BackColor = System.Drawing.Color.White;
                tr.Cells.Add(tc[0]);


                tc[1].Text = post.Title;
                tc[1].BackColor = System.Drawing.Color.White;
                tr.Cells.Add(tc[1]);


                tc[2].Text = post.Date;
                tc[2].BackColor = System.Drawing.Color.White;
                tr.Cells.Add(tc[2]);

                tc[3].Text = post.Content;
                //tr.Cells.Add(tc[3]);


                tc[4].Text = "<a href='./showPost.aspx?id=" + post.Id + "'>查看|</a><a href='./updatePost.aspx?id=" + post.Id + "'>编辑|</a><a href='./delPost.aspx?id=" + post.Id + "&pageNo =" + pageNo + "'>删除</a>";
                tc[4].BackColor = System.Drawing.Color.White;
                tr.Cells.Add(tc[4]);

                // tr.BackColor = Color.FromName("#FFFFFF");
                table.Rows.Add(tr);
            }
            TableRow pageNoTr = new TableRow();
            TableCell pageNoTc = new TableCell();
            int TotalPageNum = (postlist.Count / 5 + 1);
            String pageNoStr = "第" + pageNo + " / " + TotalPageNum + " 页";
            for (int j = 0; j < TotalPageNum; ++j)
            {
                if (j + 1 != pageNo)
                {
                    pageNoStr += "<a href='./searchPost.aspx?&pageNo=" + (j + 1) + "'>[" + (j + 1) + "]</a>&nbsp;";
                }
                else
                {
                    pageNoStr += " " + (j + 1) + " ";
                }
            }
            pageNoTc.HorizontalAlign = HorizontalAlign.Center;
            pageNoTc.Text = pageNoStr;
            pageNoTr.Cells.Add(pageNoTc);
            table.Rows.Add(pageNoTr);

            return postlist.Count;
        }

        public static Dictionary<int, string> getPostById(string id)
        {
            Dictionary<int, string> post = new Dictionary<int, string>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/xml/Post.xml"));
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("Movies").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("id") == id)
                {

                    post.Add(0, xe.GetAttribute("id"));
                    post.Add(3, xe.GetAttribute("date"));
                    post.Add(1, xe.GetAttribute("title"));
                    post.Add(4, xe.GetAttribute("content"));

//                     XmlNodeList xnl = xe.ChildNodes;
//                     foreach (XmlNode xn1 in xnl)
//                     {
//                         XmlElement xe2 = (XmlElement)xn1;
//                         if (xe2.Name == "title")
//                         {
//                             post.Add(1, xe2.InnerText);
//                         }
// 
//                         if (xe2.Name == "date")
//                         {
//                             post.Add(3, xe2.InnerText);
//                         }
// 
//                         if (xe2.Name == "content")
//                         {
//                             post.Add(4, xe2.InnerText);
//                         }
//                     }
                }

            }
            return post;
        }
        
    }
}
